package com.easysoft.framework.role.service;

import com.easysoft.commons.utils.DateTimeUtils;
import com.easysoft.commons.utils.JSONUtils;
import com.easysoft.commons.utils.UUIDUtils;
import com.easysoft.framework.annotation.jdbc.DataSource;
import com.easysoft.framework.role.mapper.RoleMenuMapper;
import com.easysoft.framework.role.mapper.RoleMenuMapperExtd;
import com.easysoft.framework.role.model.RoleMenu;
import com.easysoft.framework.role.model.RoleMenuView;
import com.easysoft.framework.role.model.UtilListModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.util.List;

@Service("service.RoleMenuService")
public class RoleMenuService {

    private final static Logger logger = LoggerFactory.getLogger(RoleMenuService.class);


    @Autowired
    @Qualifier("mapper.RoleMenuMapperExtd") 
    private RoleMenuMapperExtd roleMenuMapperExtd;

    @Autowired
    @Qualifier("mapper.RoleMenuMapper")
    private RoleMenuMapper roleMenuMapper;

    /**
     *
     * @param roleMenu
     * @return
     */
    @DataSource(DataSource.MASTER)
    public String saveRoleMenu(RoleMenu roleMenu) {
        // 生成主键
        roleMenu.setId(UUIDUtils.getUUID());
        // 保存数据
        int result = roleMenuMapper.insertSelective(roleMenu);
        // 返回主键
        if (result == 1) {
            return roleMenu.getId();
        }

        return null;
    }

    @DataSource(DataSource.MASTER)
    public int deleteRoleMenuRel(String id) {
        return roleMenuMapper.deleteByPrimaryKey(id);
    }

    @DataSource(DataSource.MASTER)
    public int updateRoleMenuRel(RoleMenu roleMenu) {
        return roleMenuMapper.updateByPrimaryKeySelective(roleMenu);
    }

    @DataSource(DataSource.SLAVE)
    public RoleMenu selectRoleMenuRelByPrimaryKey(String id) {
        return roleMenuMapper.selectByPrimaryKey(id);
    }

    /**
     * 保存角色菜单关系
     * @param roleMenuRelstr
     * @param userId
     * @param roleId
     * @return
     */
    @DataSource(DataSource.MASTER)
    public int saveList(String roleMenuRelstr, String userId, String roleId) {

        UtilListModel utilListModel = JSONUtils.json2Object(roleMenuRelstr,UtilListModel.class);
        if (utilListModel == null) {// 异常情况下直接返回保存失败
            return 0;
        }
        if (utilListModel.getRoleMenuList().size() == 0) {
            return roleMenuMapperExtd.deleteByRoleId(roleId);
        }
        //添加主键
        for (RoleMenu roleMenu : utilListModel.getRoleMenuList()) {
            roleMenu.setId(UUIDUtils.getUUID());
            roleMenu.setOperator(userId);
            roleMenu.setCreateTime(DateTimeUtils.getCurrentDate());
        }
        // 执行批量保存之前先删除该角色所拥有的权限
        roleMenuMapperExtd.deleteByRoleId(utilListModel.getRoleMenuList().get(0).getRoleId());
        return  roleMenuMapperExtd.insertRoleMenuBatch(utilListModel.getRoleMenuList());
    }

    /**
     * 获取角色对应的菜单
     * @param roleId
     * @return
     */
    @DataSource(DataSource.SLAVE)
    public List<RoleMenuView> selectRoleMenuRelByRoleId(String roleId) {
        return roleMenuMapperExtd.selectRoleMenuByRoleId(roleId);
    }


}
